<?php

namespace App\Http\Controllers;

use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    const UNSETTLEMENT = 0;

    const SETTLED = 1;

    private $_userList = [
        1 => '黄煜捷',
        2 => '雷泳龙',
        3 => '徐梦婷'
    ];

    private $_textMap = [
        'status' => [self::UNSETTLEMENT => '未结算', self::SETTLED => '已结算'],
    ];

    /**
     * 首页
     */
    public function index()
    {
        $user = Cache::get( 'user' );
        $uid = $user['uid'];
        $tmpList = DB::table( 'home_fee_info' )->where( ['creator' => 1] )->orderBy( 'create_time', 'DESC' )->get();
        foreach ( $tmpList as $value )
        {
            $temp = get_object_vars( $value );
            $temp['about_user'] = implode( '、', array_map( function( $uid ) {
                return $this->_userList[$uid];
            }, explode( ',', $temp['about_user'] ) ) );
            $list[] = $temp;
        }
        $params['list'] = isset( $list ) ? $list : [];
        $params['option'] = 'info';
        $this->textMap( $params['list'] );
        return view( 'index', $params );
    }

    /**
     * 账单列表
     */
    public function lists()
    {
        $user = Cache::get( 'user' );
        $uid = $user['uid'];
        $tmpList = DB::table( 'home_fee_info' )->where( ['creator' => 1] )->orderBy( 'create_time', 'DESC' )->get();
        foreach ( $tmpList as $value )
        {
            $temp = get_object_vars( $value );
            $temp['about_user'] = implode( '、', array_map( function( $uid ) {
                return $this->_userList[$uid];
            }, explode( ',', $temp['about_user'] ) ) );
            $list[] = $temp;
        }
        $params['list'] = isset( $list ) ? $list : [];
        $this->textMap( $params['list'] );
        return view( 'lists', $params );
    }

    /**
     * 编辑
     * @param  integer $id 数据 ID
     */
    public function edit( $id )
    {
        $params['info'] = DB::table( 'home_fee_info' )->where( ['id' => $id] )->first();
        $params['userList'] = $this->_userList;
        return view( 'edit', $params );
    }

    /**
     * 添加数据
     */
    public function add()
    {
        $params['userList'] = $this->_userList;
        return view( 'edit', $params );
    }

    /**
     * 保存数据
     */
    public function save( Request $request )
    {
        $id = $request->input( 'id' );
        $data = $request->only( ['fee', 'remark', 'about_user', 'status'] );
        $data['about_user'] = implode( ',', $data['about_user'] );
        // 更新数据
        if ( !empty( $id ) )
        {
            $data['update_time'] = time();
            $res = DB::table( 'home_fee_info' )->where( 'id', $id )->update( $data );
        }
        // 插入数据
        else
        {
            $user  = Cache::get( 'user' );
            $data = array_merge( $data, [
                'creator'       => $user['uid'], 
                'create_time'   => time(), 
                'update_time'   => time(),
            ] );
            $res = DB::table( 'home_fee_info' )->insert( $data );
        }

        if ( !$res )
            return response()->json( ['errcode' => 1, 'msg' => '保存账单数据失败'] );
        else
            return response()->json( ['errcode' => 0, 'msg' => '保存账单数据成功'] );
    }

    private function textMap( &$list, $column = 'status' )
    {
        foreach ( $list as &$value )
        {
            if ( isset( $value[$column] ) )
            {
                $value[$column . '_text'] = $this->_textMap[$column][$value[$column]];
            }
        }
    }

    /**
     * 微信绑定页面
     */
    public function bind( $openid )
    {
        $openidInfo = DB::table( 'home_wx_bind' )->where( 'openid', $openid )->first();
        if ( !empty( $openidInfo ) )
            return redirect()->action( 'HomeController@index' );

        $bindList = DB::table( 'home_wx_bind' )->get();
        $userList = $this->_userList;
        foreach ( $bindList as $bind )
        {
            if ( !empty( $openid ) )
                unset( $userList[$bind->uid] );
        }
        $params['userList'] = $userList;
        $params['openid'] = $openid;
        return view( 'bind', $params );
    }

    /**
     * 微信绑定接口
     */
    public function binding( Request $request )
    {
        $data = $request->only( ['openid', 'uid'] );
        $uidInfo = DB::table( 'home_wx_bind' )->where( 'uid', $data['uid'] )->first();
        if ( !empty( $uidInfo ) )
            return response()->json( ['errcode' => 1, 'msg' => '该用户已被绑定'] );
        $openidInfo = DB::table( 'home_wx_bind' )->where( 'openid', $data['openid'] )->first();
        if ( !empty( $openidInfo ) )
            return response()->json( ['errcode' => 1, 'msg' => '你已经绑定过'] );

        if ( !DB::table( 'home_wx_bind' )->insert( $data ) )
            return response()->json( ['errcode' => 1, 'msg' => '绑定失败，请重试'] );
        else
            return response()->json( ['errcode' => 0, 'msg' => '绑定成功'] );
    }
}
